package com.xlh.service.courseTest.impl;

import com.xlh.dao.courseTest.CourseDuplicateMapper;
import com.xlh.param.courseTest.DuplicateParam;
import com.xlh.pojo.courseTest.CourseDuplicate;
import com.xlh.pojo.courseTest.CourseDuplicateExample;
import com.xlh.service.courseTest.CourseDuplicateService;
import com.xlh.util.BeanUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhr 2019/4/13
 */
@Service
public class CourseDuplicateServiceImpl implements CourseDuplicateService {

    @Autowired
    private CourseDuplicateMapper mapper;

    @Override
    public CourseDuplicate getDuplicate(Long courseId) {
        CourseDuplicateExample example = new CourseDuplicateExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        List<CourseDuplicate> list = mapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public void setDuplicate(Long courseId, DuplicateParam param) {
        CourseDuplicate duplicate = BeanUtil.transform(CourseDuplicate.class, param);
        if (duplicate == null) return;
        duplicate.setCourseId(courseId);

        CourseDuplicateExample example = new CourseDuplicateExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        if (mapper.countByExample(example) > 0) {
            mapper.updateByExampleSelective(duplicate, example);
        } else {
            mapper.insertSelective(duplicate);
        }
    }

    @Override
    public void deleteDuplicate(Long courseId) {
        CourseDuplicateExample example = new CourseDuplicateExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        mapper.deleteByExample(example);
    }
}
